package com.mingqi.enetconnected.primary.mytask.taskDetails;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseFragment;
import com.mingqi.enetconnected.data.bean.order.GoodsOrder;
import com.mingqi.enetconnected.primary.mytask.activity.SelectGoodsActivity;
import com.mingqi.enetconnected.primary.mytask.adapter.ServiceContentAdapter;
import com.mingqi.enetconnected.primary.mytask.entity.MultipleEntity;
import com.mingqi.enetconnected.util.hint.ToastUtils;
import com.mingqi.enetconnected.view.Dialog.BottomDialog;
import com.mingqi.enetconnected.view.Dialog.GoodsEditDialog;
import com.mingqi.enetconnected.view.Dialog.ServiceEditDialog;
import com.mingqi.enetconnected.view.Dialog.tree.DynamicTreePopup;
import com.mingqi.enetconnected.view.treechoose.bean.SecondLevel;
import com.mingqi.enetconnected.view.treechoose.bean.ThirdLevel;
import com.mingqi.enetconnected.view.treechoose.bean.FirstLevel;
import com.mingqi.enetconnected.view.treechoose.bean.FourthLevel;
import com.mingqi.enetconnected.view.treechoose.listener.OnDialogCloseListener;
import com.mingqi.enetconnected.view.treechoose.listener.OnTreeChooseCompleteListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 服务内容页
 * Created by li on 2018/8/13.
 * WeChat 18571658038
 * author LiWei
 */
public class ServiceContentFragment extends BaseFragment implements ServiceContentAdapter.OnOperateListener {

    private static final String TAG = ServiceContentAdapter.class.getSimpleName();
    private BottomDialog dialog;
    private TextView mView;

    @BindView(R.id.fragment_service_content)
    RecyclerView mServiceContentRecycler;
    @BindView(R.id.service_content_container)
    LinearLayout mContentContainer;

    private ServiceContentAdapter mAdapter;

    @Override
    protected View bindView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_service_content, container, false);
        return view;
    }

    private void showTreeChoose() {

        if (dialog != null) {
            dialog.show();
        } else {

            dialog = new BottomDialog(mContext);
            dialog.setOnTreeChooseCompleteListener(new OnTreeChooseCompleteListener() {
                @Override
                public void onChooseComplete(FirstLevel firstLevel, SecondLevel secondLevel, ThirdLevel thirdLevel, FourthLevel fourthLevel) {

                    //选完后回调
                    String firstCode = (firstLevel == null ? "" : firstLevel.code);
                    String secondCode = (secondLevel == null ? "" : secondLevel.code);
                    String thirdCode = (thirdLevel == null ? "" : thirdLevel.code);
                    String fourthCode = (fourthLevel == null ? "" : fourthLevel.code);

                    String s = (firstLevel == null ? "" : firstLevel.name) + (secondLevel == null ? "" : secondLevel.name) + (thirdLevel == null ? "" : thirdLevel.name) +
                            (fourthLevel == null ? "" : fourthLevel.name);
                    mView.setText(s);

                    Log.d("--TAG--", "ServiceContentFragment onChooseComplete()" + firstCode + "\n" + secondCode
                     + "\t" + thirdCode + "\r\n" + fourthCode);

                    ToastUtils.showHint("选择了：" + firstCode + " " + secondCode + " " + thirdCode + " " + fourthCode,mView);
                }
            });

            dialog.setDialogDismissListener(new OnDialogCloseListener() {
                @Override
                public void onDialogClose() {

                    if(dialog!=null){
                        dialog.dismiss();
                    }
                }
            });

            dialog.setTvTabTextSize(15);
            dialog.setIndicatorBackgroundColor(R.color.colorPrimary);
            dialog.serColorTvTabChoose(R.color.black);
            dialog.serColorTvTabNormal(R.color.tv_normal);
            // TODO: 2018/8/20 根据code和下标设置选中数据
//            dialog.setDisplayChooseTree("1",0,"11",0,"111",0,"1111",0);
            dialog.show();
        }
    }

    @Override
    protected void initView() {
        List<MultipleEntity> lists = new ArrayList<>();
        lists.add(new MultipleEntity(MultipleEntity.ServiceContentEnum.SERVICE_ITEM.ordinal(),"服务内容"));
        lists.add(new MultipleEntity(MultipleEntity.ServiceContentEnum.GOODS_INFO.ordinal(),"耗材信息"));
        lists.add(new MultipleEntity(MultipleEntity.ServiceContentEnum.PRESSURE_TEST.ordinal(),"压力测试"));

        mAdapter = new ServiceContentAdapter(lists);
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mServiceContentRecycler.setLayoutManager(manager);
        mServiceContentRecycler.setAdapter(mAdapter);

        mAdapter.setOnOperateListener(this);

    }

    @Override
    protected void initData() {

        List<MultipleEntity> lists = new ArrayList<>();
        List<GoodsOrder> serviceContent = getOrderByType(null, null);
        List<GoodsOrder> goodsInfo = getOrderByType(null, null);


        lists.add(new MultipleEntity<>(MultipleEntity.ServiceContentEnum.SERVICE_ITEM.ordinal(), "服务内容", serviceContent));
        lists.add(new MultipleEntity<>(MultipleEntity.ServiceContentEnum.GOODS_INFO.ordinal(), "耗材信息", goodsInfo));
        lists.add(new MultipleEntity(MultipleEntity.ServiceContentEnum.PRESSURE_TEST.ordinal(),"压力测试"));

        mAdapter.setNewData(lists);

    }



    public List<GoodsOrder> getOrderByType(String orderCode, MultipleEntity.ServiceContentEnum type){
        List<GoodsOrder> result = new ArrayList<>();
        if (type == MultipleEntity.ServiceContentEnum.SERVICE_ITEM){

        }else if (type == MultipleEntity.ServiceContentEnum.GOODS_INFO){

        }

        for (int i = 0; i < 4; i++) {
            GoodsOrder order = new GoodsOrder();
            order.setProductName("厨房燃气-煤气灶");
            order.setTotalFee(30000);
            order.setPaidFee(25000);
            order.setNum(3);
            result.add(order);
        }
        return result;
    }


    @Override
    public GoodsOrder onServiceContentAddTo() {
//        GoodsOrder order = new GoodsOrder();
//        order.setProductName("厨房燃气-天然气");
//        order.setTotalFee(10000);
//        order.setPaidFee(4532);
//        order.setNum(12);

        new DynamicTreePopup(mContext,false).buildView().setTitle("服务项目").show(mContentContainer);


        return null;
    }

    @Override
    public GoodsOrder onInventorySelection() {
        new DynamicTreePopup(mContext,true).buildView().setTitle("请选择耗材").show(mContentContainer);
        return null;
    }

    @Override
    public GoodsOrder onSelectTemplate() {
        startActivity(SelectGoodsActivity.class);
        return null;
    }

    @Override
    public void onRequestFocus() {
        mContentContainer.requestFocus();

        // TODO: 2018/9/14 hide input
    }


    @Override
    public void showServiceEditDialog(int position,GoodsOrder order) {
        new ServiceEditDialog(mContext, new ServiceEditDialog.OnServiceItemAddCallBack() {
            @Override
            public void onServiceItemAdd(double value) {

            }

            @Override
            public void onIllegalInput() {

            }
        }).show();
    }

    @Override
    public void showGoodsEditDialog(final int position, GoodsOrder order) {

        new GoodsEditDialog(mContext, order,new GoodsEditDialog.OnGoodsItemAddCallBack() {
            @Override
            public void onGoodsAdd(GoodsOrder order) {
                mAdapter.setGoodsData(position,order);
            }
        }).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdapter.removeOperateListener();
    }
}
